package cn.csg.ams.collect2.poptopics.redisPool;


import cn.csg.ams.collect2.poptopics.infrastructure.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 全链路计数池
 * 按供电所编码，统计各个阶段的数据量
 */
@Component
public class LinkCountPool {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 告警总数量key
     */
    public static final String ALARM_TOTAL_NUM_KEY = "link_alarm_total_num_";

    /**
     * 告警及时数量key
     */
    public static final String ALARM_TIMELY_NUM_KEY = "link_alarm_timely_num_";

    /**
     * 告警清洗数量key
     */
    public static final String ALARM_CLEAN_NUM_KEY = "link_alarm_clean_num_";

    /**
     * 负荷总数量key
     */
    public static final String LOAD_TOTAL_NUM_KEY = "link_load_total_num_";

    /**
     * 负荷及时数量key
     */
    public static final String LOAD_TIMELY_NUM_KEY = "link_load_timely_num_";

    /**
     * 负荷清洗数量key
     */
    public static final String LOAD_CLEAN_NUM_KEY = "link_load_clean_num_";

    /**
     * 告警总数量+1
     */
    public void incrementNum(String keyPre, String supplyOrgNo) {
        String key = keyPre + DateUtil.format(new Date(), "yyyy-MM-dd");
        redisTemplate.opsForHash().increment(key, supplyOrgNo, 1);
    }


    /**
     * 获取数量
     * key为供电所编码， value为数量
     */
    public Map<String, Long> getNum(String keyPre, Date date) {
        String key = keyPre + DateUtil.format(date, "yyyy-MM-dd");
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);

        Map<String, Long> result = new HashMap<>(16);
        for (Map.Entry<Object, Object> entry : entries.entrySet()) {
            result.put(entry.getKey().toString(), Long.valueOf(entry.getValue().toString()));
        }
        return result;
    }

}
